------------------ fgui require start ------------------
------------------  fgui require end  ------------------
local GuildListItemSkin = Class("GuildListItemSkin");

function GuildListItemSkin:ctor(com)
    ------------------ fgui事件绑定start ------------------
    self:bindChild(com);
    self.btn_Join.onClick:Add(self.ClickJoinNow, self);
    self:init();
    ------------------  fgui事件绑定end  ------------------
end

function GuildListItemSkin:bindChild(com)
    ------------------ fgui变量绑定start ------------------
    self.fguiObj = com;
    self.ldr_flag = com:GetChild("ldr_flag");
    self.text_numberofpeople = com:GetChild("Text_numberofpeople");
    self.text_lv = com:GetChild("Text_lv");
    self.text_guildname = com:GetChild("Text_guildname");
    self.text_address = com:GetChild("Text_address");
    self.text_combateffectiveness = com:GetChild("Text_combateffectiveness");
    self.btn_Join = com:GetChild("Btn_Joinnow")
    self.applyCtrl = com:GetController("isApply")
    self.haveGuildCtrl = com:GetController("Ctl_haveGuild")
    ------------------  fgui变量绑定end  ------------------
end

function GuildListItemSkin:init()
    self.data = {}
end

function GuildListItemSkin:getUnionId()
    return self.data.id;
end

function GuildListItemSkin:ClickJoinNow()
    logicMgr.GuildManager.canClick = false
    self.applyCtrl.selectedIndex = 1
    self.btn_Join.title = getLanguage("GuildInvestment_1055")
    local memCnt = self.data.memberCount
    local maxCnt = logicMgr.GuildManager.getMaxMemberCnt(self.data.lv);
    --if memCnt >= maxCnt then
    --    UISysTips.AddMsg(getLanguage("GuildInvestment_1073"));
    --    return
    --end

    local cdTime = logicMgr.GuildManager.cdTime;
    if cdTime then
        cdTime = tonumber(tostring(cdTime));
        local curServerTime = HelperFunc.GetServerTime(false);
        local disTime = math.ceil(cdTime-curServerTime);
        if disTime > 0 then
            local cdStr = HelperFunc.GetSurpTimeStr(disTime / 1000);
            local tipStr = string.format("加入战团冷却中,%s后可以申请加入",cdStr);
            UISysTips.AddMsg(tipStr);
            logicMgr.GuildManager.canClick = true
            return;
        end
    end
    unionHandler.reqJoinUnion(self.data.id);
    logicMgr.GuildManager.setSelfApplyGuildList({unionIds={self.data.id}});
end

function GuildListItemSkin:OnDestroy()
    ------------------ fgui事件移除start ------------------
    self:beforeDestroy();
    ------------------  fgui事件移除end  ------------------
end

------------------  ↑ fgui事件句柄 ↑  ------------------

function GuildListItemSkin:onRender(data)
    if data == nil or data.unionSetting == nil then
        return
    end
    self.data = data;
    if logicMgr.GuildManager.checkSelfApplyGuildId(self.data.id) then
        self.applyCtrl.selectedIndex = 1
        self.btn_Join.title = getLanguage("GuildInvestment_1055")
    else
        self.applyCtrl.selectedIndex = 0
        self.btn_Join.title = getLanguage("GuildInvestment_1052")
    end
    if logicMgr.GuildManager.unionInfo then
        self.haveGuildCtrl.selectedIndex = 1    --自己有工会时不显示申请按钮
    else
        self.haveGuildCtrl.selectedIndex = 0
    end
    self.text_guildname.text = data.name;
    self.text_lv.text = string.format(getLanguage("GuildInvestment_1119"), data.lv);
    self.text_combateffectiveness.text = data.liveness .. '';
    if self.data.unionSetting.city then
        self.text_address.text = self.data.unionSetting.province .. " " .. self.data.unionSetting.city;
    end
    self.text_numberofpeople.text = data.memberCount .. "/" .. logicMgr.GuildManager.getMaxMemberCnt(data.lv);
    self.ldr_flag.url = logicMgr.GuildManager.getFlagUrl(self.data.unionSetting.flagId);
end

function GuildListItemSkin:beforeDestroy()
end

return GuildListItemSkin;
